﻿namespace MA.MembershipProvider
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Xml;

    public partial class AdminAppConfig : Form
    {
        public AdminAppConfig()
        {
            InitializeComponent();
        }

        private void AdminAppConfig_Load(object sender, EventArgs e)
        {
            StringBuilder Con = new StringBuilder("Data Source=");
            Con.Append("");
            Con.Append(";Initial Catalog=");
            Con.Append("");
            Con.Append(";Integrated Security=SSPI;");


            cbPasswordFormat.Text = "Hashed";
            cbDefaultProvider.Text = "SqlProvider";
            cbOfType.Text = "System.Web.Security.SqlMembershipProvider";
            txtConnctionString.Text = Con.ToString();
        }

        public void updateConfigFile()
        {
            //updating config file
            XmlDocument XmlDoc = new XmlDocument();
            //Loading the Config file
            XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            foreach (XmlElement xElement in XmlDoc.DocumentElement)
            {
                if (xElement.Name == "system.web")
                {
                    if (xElement.FirstChild.LastChild.LastChild.Name == "add")
                    {
                        // Node membership
                        xElement.FirstChild.Attributes["userIsOnlineTimeWindow"].Value = txtuserIsOnlineTimeWindow.Text;
                        xElement.FirstChild.Attributes["defaultProvider"].Value = cbDefaultProvider.Text;

                        // Node system.web / Membership / Providers / Add 
                        xElement.FirstChild.LastChild.LastChild.Attributes["name"].Value = txtNameProvider.Text;
                        Properties.Settings.Default.NombreProvedor = txtNameProvider.Text;
                        Properties.Settings.Default.Save();

                        xElement.FirstChild.LastChild.LastChild.Attributes["type"].Value = cbOfType.Text;
                        xElement.FirstChild.LastChild.LastChild.Attributes["connectionStringName"].Value = "DefaultConnection";
                        xElement.FirstChild.LastChild.LastChild.Attributes["applicationName"].Value = Properties.Settings.Default.NombreAplicacion;
                        xElement.FirstChild.LastChild.LastChild.Attributes["enablePasswordRetrieval"].Value = cbEnablePasswordRetrieval.Checked.ToString();
                        xElement.FirstChild.LastChild.LastChild.Attributes["enablePasswordReset"].Value = cbEnablePasswordReset.Checked.ToString();
                        xElement.FirstChild.LastChild.LastChild.Attributes["requiresQuestionAndAnswer"].Value = cbRequiresQuestionAndAnswer.Checked.ToString();
                        xElement.FirstChild.LastChild.LastChild.Attributes["requiresUniqueEmail"].Value = cbRequiresUniqueEmail.Checked.ToString();
                        xElement.FirstChild.LastChild.LastChild.Attributes["minRequiredPasswordLength"].Value = txtMinRequiredPasswordLength.Text;
                        xElement.FirstChild.LastChild.LastChild.Attributes["minRequiredNonalphanumericCharacters"].Value = txtMinRequiredNonalphanumericCharacters.Text;
                        xElement.FirstChild.LastChild.LastChild.Attributes["passwordFormat"].Value = cbPasswordFormat.Text;
                    }
                }

                if (xElement.Name == "connectionStrings")
                {
                    xElement.FirstChild.Attributes["name"].Value = "DefaultConnection";
                    xElement.FirstChild.Attributes["connectionString"].Value = txtConnctionString.Text;
                }

            }
            //writing the connection string in config file
            XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }

        private void btoIniciar_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.NombreProvedor = txtNameProvider.Text;
            Properties.Settings.Default.NombreAplicacion = txtNameApplication.Text;
            Properties.Settings.Default.ConnectionString = txtConnctionString.Text;
            Properties.Settings.Default.DelTipo = cbOfType.Text;
            //Properties.Settings.Default.WebConf = "";
            Properties.Settings.Default.Save();

            updateConfigFile();

            frmDefault frmDefault = new frmDefault();
            this.Visible = false;
            frmDefault.Show();
        }

    }
}
